package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import com.ruoyi.system.api.client.NewsClient;
import com.ruoyi.system.api.domain.News;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
@RequiredArgsConstructor
public class SysNoticeServiceImpl implements ISysNoticeService {
    private static final Logger log = LoggerFactory.getLogger(SysNoticeServiceImpl.class);
    @Autowired
    private SysNoticeMapper noticeMapper;
    public final NewsClient newsClient;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNotice(SysNotice notice) {
        // 插入通知
        int count = noticeMapper.insertNotice(notice);

        // 转换对象 copyBeanProp在下面定义
        News news = copyBeanProp(notice);
        if (news == null) {
            log.error("转换 SysNotice 到 News 失败，news 对象为空");
            return count; // 或者抛出异常，视业务需求而定
        }

        // 添加新闻
        try {
            newsClient.add(news);
        } catch (Exception e) {
            log.error("调用 newsClient.add(news) 异常", e);
            // 可选：抛出异常以触发事务回滚
            throw new RuntimeException("同步新闻失败", e);
        }

        return count;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        // 更新通知信息
        int count = noticeMapper.updateNotice(notice);
        notice.setUpdateBy(SecurityUtils.getUsername());
        try {
            News news = copyBeanProp(notice);
            news.setUpdateBy(SecurityUtils.getUsername());
            LocalDateTime currentTime = LocalDateTime.now();
            news.setUpdateTime(currentTime);
            newsClient.edit(news);
        } catch (Exception e) {
            log.error("调用 newsClient.edit(news) 异常", e);
            throw new RuntimeException("同步修改通知公告失败", e);
        }
        return count;
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        // 删除新闻
        try {
            newsClient.remove(new Long[]{noticeId});
        } catch (Exception e) {
            log.error("Failed to delete news via newsClient.", e);
            // 可选：根据业务需求决定是否抛出异常或触发补偿机制
            throw new RuntimeException("同步删除失败", e);
        }

        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        try {
            newsClient.remove(noticeIds);
        } catch (Exception e) {
            log.error("调用 newsClient.remove(news) 异常", e);
            throw new RuntimeException("同步删除通知公告失败", e);
        }
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }
    public News copyBeanProp(SysNotice notice) {
        News news = new News();
        news.setNewsId(notice.getNoticeId());
        news.setTitle(notice.getNoticeTitle());
        news.setTypeId(6);
        news.setContent(notice.getNoticeContent());
        news.setDelayTime(0);
        news.setSource("通知公告");
        news.setStatus(Integer.valueOf(notice.getStatus()));
        news.setRemark(notice.getRemark());
        return news;
    }
}
